/**
 * Created by Administrator on 2017/7/4 0004.
 */
require(["jquery", "paging", "bootstrap", "bbcare", "jquery.dataTables", 'bootstrap.dataTables',
    "scprit/assess/app/gesell/common.js",
    "oms_config/root"
], function($, Paging, bootstrap, $bb, $jTable, $bTable, $common, $root) {
    var URL = $bb.baseUrl("followapp");
    var obj = $bb.getPageParams();
    var userId = obj.babyId;
    var type = 'Gesell'; //历史记录传参；
    var patientSchemeId = obj.patientSchemeId;
    var count = 1,
        pagesize = 20;
    var HashMap = $common.hashmap(),
        hashMap = new HashMap();
    var historyTitle = $("#historyTitle");

    getBabyHistoryList();
    var p = new Paging();
    p.init({
        target: '#pageTool',
        pagesize: pagesize,
        count: count,
        toolbar: false,
        callback: function(page, size, count) {
            getBabyHistoryList();
        }
    });

    // $("#dataList").on('click', "ul[result]", function() {
    //     var data = eval("(" + hashMap.get($(this).attr("result")).replace(/\=/g, "\:") + ")");
    //     var doctorName = $(this).attr("doctorName");
    //     var createTime = $(this).attr("testDate");
    //     obj.createTime = createTime;
    //     obj.doctorName = doctorName;
    //     localStorage.setItem("GesellIntelligence", JSON.stringify(data));
    // });
    $("#dataList").on('click', "ul[result] li span", function() {
        // var data = eval("(" + hashMap.get($(this).parent().parent().attr("result")).replace(/\=/g, "\:") + ")");
        var data = hashMap.get($(this).parent().parent().attr("result"));

        var doctorName = $(this).parent().parent().attr("doctorName");
        var createTime = $(this).parent().parent().attr("testDate");
        obj.createTime = createTime;
        obj.doctorName = doctorName;
        localStorage.setItem("GesellIntelligence", JSON.stringify(data));
        if ($(this).attr('class') == "color-green cursor-pointer gesellHistoryList-questions") {
            var result = $(this).parent().parent().attr("answerResult");
            $.extend(obj, { 'result': result });
            $bb.openPage("gesellDetail", obj);
        } else {
            $bb.openPage("gesellHistoryRecord", obj);
        }
    });

    function getBabyHistoryList() {
        var params = {};
        params = {
            "channelType": "2",
            "evaluationType": type,
            "queueUserId": userId
        }
        historyTitle.html("Gesell智力发育评估历史记录");
        $bb.ajax({
            url: URL + "service/evaluation/recordlist", //baby地址
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.rows) {
                    hashMap = new HashMap();
                    count = data.total;
                    var babyData = data.rows;
                    if (count == 0) {
                        $bb.toastShow("患者还未参与该项测评！", 1500);
                    }
                    var str = '';
                    for (var i = 0; i < data.rows.length; i++) {
                        var testDate = data.rows[i].createTime.substring(0, 10);
                        var gender = data.rows[i].gender == 'M' ? '男' : '女';
                        var acturalDay = $common.DateDiff(testDate, data.rows[i].birthday);
                        var age = $common.age(acturalDay, data.rows[i].pregnancyWeek);
                        var monthAge = $common.monthAge(testDate, data.rows[i].pregnancyWeek);

                        hashMap.put(data.rows[i].id, data.rows[i]);
                        var pretermBirth = 37 * 7 > data.rows[i].pregnancyWeek ? "是" : "否";
                        var weight = data.rows[i].weight ? data.rows[i].weight + "(g)" : "未知";
                        var doctorName = data.rows[i].doctorName == undefined ? "未知" : data.rows[i].doctorName;
                        str += "<ul class='clearfix' answerResult='" + data.rows[i].result + "' result='" + data.rows[i].id + "' testDate = '" + testDate + "' doctorName = '" + doctorName + "' >";
                        str += '<li>' + data.rows[i].userName + '</li>';
                        str += '<li>' + data.rows[i].phoneNo + '</li>';
                        str += '<li>' + gender + '</li>';
                        str += '<li>' + age + '</li>';
                        str += '<li>' + data.rows[i].createTime + '</li>';
                        str += '<li><span class="color-green cursor-pointer gesellHistoryList-questions">评估问卷</span><span class="color-green cursor-pointer ml8 gesellHistoryList-report">评估报告</span></li>';
                        str += '</ul>';
                    }
                    $("#dataList").html(str);
                }
            }
        });
    }
});